package com.cz.mes;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.cz.dto.OrderSync;

import java.io.File;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class MPRLog {
    public static void main(String[] args) {
        List<String> saleOrderIds = readFromConfig();

        List<OrderSync> orderSyncList = new ArrayList<>();
        AtomicInteger i = new AtomicInteger();

        saleOrderIds.forEach(sale_bill_no -> {
            try {
                OrderSync orderSync = new OrderSync();

                String sql = "select modt.id,modt.plan_id,modt.create_on,mo.bill_no,modt.sale_bill_no from manufacture_order_detail modt left join manufacture_order mo on mo.id = modt.main_id where modt.sale_bill_no = :sale_bill_no" +
                        " and modt.kingdee_data is null";
                Map<String, Object> queryParam = new HashMap<>();
                queryParam.put("sale_bill_no", sale_bill_no);
                Entity manufactureOrderDetail = Db.use("mes-prod-slave").queryOne(sql, queryParam);
                if(manufactureOrderDetail == null){
                    return;
                }
                String billNo = manufactureOrderDetail.getStr("bill_no");
                int detailId = manufactureOrderDetail.getInt("id");


                String planId = StrUtil.blankToDefault(manufactureOrderDetail.getStr("plan_id"),"");
                String createTime = DateUtil.formatDateTime(manufactureOrderDetail.getDate("create_on"));
                String saleOrderNo = manufactureOrderDetail.getStr("sale_bill_no");

                String info = createTime+" "+billNo + " " + detailId + " " + planId + " " + saleOrderNo;

                /**
                sql = "select id from interface_call_log where request_param like :request_param ";
                Map<String,Object> queryMapRequestParam = new HashMap<>();
                queryMapRequestParam.put("request_param", "%\"planId\":" + planId + "%");
                Entity entity = Db.use("mes-prod-slave").queryOne(sql, queryMapRequestParam);
                if (entity != null) {
                    orderSync.setHasPushInfo("有");
                    //orderSync.setPushInfo(entity.getStr("request_param"));
                }else {
                    orderSync.setHasPushInfo("");
                    //orderSync.setPushInfo("");
                }

                 **/


                /**
                sql = "select id from interface_call_log where interface_call_log.response_result like :response_result ";
                Map<String,Object> queryMapResponseParam = new HashMap<>();
                queryMapResponseParam.put("response_result", "%\"planId\":" + planId + "%");
                entity = Db.use("mes-prod-slave").queryOne(sql, queryMapResponseParam);
                if (entity != null) {
                    orderSync.setHasPushInfoResult("有");
                    //orderSync.setPushInfoResult(entity.getStr("response_result"));
                }else {
                    orderSync.setHasPushInfoResult("");
                    //orderSync.setPushInfoResult("");
                }
                **/
                orderSync.setManufactureOrderBillNO(billNo);
                orderSync.setManufactureOrderDetailId(detailId);
                orderSync.setPlanId(String.valueOf(planId));
                orderSync.setSaleOrderNo(saleOrderNo);
                orderSync.setCreateTime(createTime);
                orderSyncList.add(orderSync);
                System.out.println(i.incrementAndGet());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        });
        File mrpFile = new File("d:\\mrp.xlsx");
        if (mrpFile.exists()) {
            mrpFile.delete();
        }
        BigExcelWriter writer = ExcelUtil.getBigWriter(mrpFile);
        writer.write(orderSyncList, true);
        writer.close();

    }

    public static List<String> readFromExcel() {
        ExcelReader reader = ExcelUtil.getReader("C:\\Users\\root\\Desktop\\20250512.xlsx");
        List<List<Object>> readAll = reader.read();
        // 去除第一条记录
        if (!readAll.isEmpty()) {
            readAll.remove(0);
        }
        List<String> saleOrderIds = readAll.stream().map(item -> String.valueOf(item.get(0))).collect(Collectors.toList());
        return saleOrderIds;
    }

    public static List<String> readFromConfig() {
        List<String> sourceOrderIds = new ArrayList<>(Arrays.asList(
                "ATHT2506030186-1",
                "ATHT2506030189-2",
                "ATHT2506030189-3",
                "ATHT2506030189-4",
                "ATHT2506030190-1",
                "ATHT2506030190-2",
                "ATHT2506030282-2",
                "ATHT2506030282-3",
                "ATHT2506030282-4",
                "ATHT2506030282-5",
                "ATHT2506030282-6",
                "ATHT2506030282-7",
                "ATHT2506030282-9"
        ));
        return sourceOrderIds;
    }
}
